<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @author Garry
 * @date 01/5/2011
 * @desc Display classes carry info to the views
 */
class Display {
	public $base = "";
	/**
	 *
	 * this is the parent page element
	 * @var Display
	 */
	public $wrapper;
	/**
	 *
	 * Needed to be class wide
	 * @var array
	 */
	protected static $scripts = array();
	/**
	 * Needed to be class wide
	 * @var array
	 */
	protected static $styles = array();
	/**
	 *
	 * Needed to be class wide
	 * @var unknown_type
	 */
	protected static $initialisedJavaScript = array();
	/**
	 *
	 * This is the output values of the display.
	 * It could be a simple attribute, a component or html
	 * @var unknown_type
	 */
	public $output;
	/**
	 * 
	 * Construct the display
	 */
	public function __construct( $base = NULL){
		if( isset($base) ){
			$this->base = $base;
		}
	}
	/**
	 *
	 * adds a child display to this composite
	 * @param Display $display
	 */
	public function addChild( Display  $display,$token = "" ){
		// might not be defined
		if( empty($display->output) ){
			$display->output = new stdClass();
		}
		foreach( $display->output as $key => $val ){
			if( !empty( $this->$key ) ){
				if( is_array($this->$key) ){
					array_push($this->$key, $val);
				}
				else {
					$temp = $this->$key;
					$this->$key = array();
					array_push($this->$key, $temp);
					array_push($this->$key, $val);
				}
			}
			else {
				$this->$key =$val;
			}
		}
		if( !empty($token)){
			$this->$token = $display->output->content;
		}

	}

	public function addJSInit($lines){
		if( !in_array($lines, Display::$initialisedJavaScript)){
					Display::$initialisedJavaScript[]=$lines;
				}
	}
	/**
	 *
	 * gets a property if it exists
	 * @param unknown_type $prop
	 */
	public function __get( $prop ){
		if( empty( $this->output ) ){
			$this->output = new stdClass();
		}
		return $this->output->$prop;
	}

	/**
	 *
	 * sets the property with a value
	 * @param unknown_type $prop
	 * @param unknown_type $val
	 */
	public function __set( $prop, $val){
		if( empty( $this->output) ){
			$this->output = new stdClass();
		}
		if( ($prop=="scripts"||$prop=="styles") ){
			if($prop=="scripts"){ 
				if( !in_array($val, Display::$scripts)){
					Display::$scripts[]=$val;
				}
			}
			if($prop=="styles"){
				if( !in_array($val, Display::$styles)){
					Display::$styles[]=$val;
				}
			}
			return;
		}
		if( !empty( $this->output->$prop )&& $prop != "content"  ){
			if( is_array($this->output->$prop) ){
				// check to see if this is not already here if script or stylesheet
				array_push($this->output->$prop, $val);
			}
			else {
				$temp = $this->output->$prop;
				$this->output->$prop = array();
				array_push($this->output->$prop, $temp);
				array_push($this->output->$prop, $val);
			}
		}
		else {
			$this->output->$prop = $val;
		}
	}

	protected function PropertyExists( $aProps,$prop,$val ){
		if( is_array( $aProps ) ){
				
		}
	}
	/**
	 *
	 * Assembles all child displays plus own view
	 */
	public function finalize(){
		if( !empty($this->base)){
			$this->content = get_instance()->load->view($this->base,$this->output,TRUE);
		}
	}
}


?>